package com.example.yoho.mvp.presenter;

import android.annotation.SuppressLint;

import com.example.lib_core.base.BaseEntity;
import com.example.lib_core.mvp.presenter.BasePresenter;
import com.example.yoho.mvp.contract.ShoesTrandContract;
import com.example.yoho.mvp.model.entity.CouponlistEntity;
import com.example.yoho.mvp.model.entity.ShoesBean;

import org.json.JSONException;
import org.json.JSONObject;

import javax.inject.Inject;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class ShoesTranPresenter extends BasePresenter<ShoesTrandContract.Model,ShoesTrandContract.View> {

    private int page=1;
    private int user_id=1;


    @Inject
    public ShoesTranPresenter() {
    }

    @SuppressLint("CheckResult")
    public void requestShoesAll(){
        this.page=page;
        this.user_id=user_id;
        JSONObject shoesJob = new JSONObject();
        JSONObject couponJob = new JSONObject();

        try {
            shoesJob.put("page","1");
            couponJob.put("user_id","1");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        final Observable<BaseEntity> merge = Observable.merge(mModel.shoesTrandModel(shoesJob.toString()),
                mModel.couponlistModel(couponJob.toString()));
      merge.observeOn(AndroidSchedulers.mainThread())
              .subscribeOn(Schedulers.io())
              .subscribe(new Observer<BaseEntity>() {
                  @Override
                  public void onSubscribe(Disposable d) {
                      disposables.add(d);
                      mView.showDialog();
                  }

                  @Override
                  public void onNext(BaseEntity baseEntity) {
                        if (baseEntity instanceof ShoesBean){
                            ShoesBean shoesBean= (ShoesBean) baseEntity;
                            mView.showShoesData(shoesBean);
                        }else if (baseEntity instanceof CouponlistEntity){
                            CouponlistEntity couponlistEntity= (CouponlistEntity) baseEntity;
                            mView.showCouponlistData(couponlistEntity);
                        }
                  }

                  @Override
                  public void onError(Throwable e) {
                      mView.showToast("请求出错"+e.getMessage());
                  }

                  @Override
                  public void onComplete() {
                      mView.hideDialog();
                  }
              });

    }
}
